
function SubmitOrder(topay) {


	if(localStorage.userToken==undefined){
		console.log("请登陆") 
	}


	if(topay== "Wechat"){
		$.ajax({
 			type:"post",
 			contentType: "application/json", //必须有
  			url: URL_all+'client/orderinfo/generateorder',
  			headers:{'userToken':localStorage.userToken},
  			data:"{'orderinfo':{'orderAddress':'深圳罗湖','paymentMethod':'Wechat','orderAdcode':'100000'},"
			+"'commodity':[{'commodityNumber':'1522633738868','buyAmount':'1'}],"
			+"'incidental':[]}",
 			success:function(data){
				/*var data = JSON.parse(obj);*/
				/*console.log(obj.dataResultObj);*/
				if (typeof WeixinJSBridge == "undefined"){
				   if( document.addEventListener ){
				       document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
				   }else if (document.attachEvent){
				       document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
				       document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
				   }
				}else{
					$("#ordernumber").val(data.dataResultObj.dataResultMap.orderNumber)
					$("#restnumber").val(data.dataResultObj.dataResultMap.orderNumber)
					$("#tko").val(data.dataResultObj.dataResultMap.orderNumber)
					
					 onBridgeReady(data);
					
					/*if(data.htmlState=="Success"){
						
					}
					else{
						  console.log(
						  "error")
					}*/
				}
			},
			error:function(xhr,type,errorThrown){
				//异常处理；
				console.log(type);
			}
       });
	}

	if(topay== "AliPay"){
		$.ajax({
		  	type:"post",
		  	contentType: "application/json", //必须有
		  	url: URL_all+'client/orderinfo/generateorder',
		    dataType:"json",
		 	headers:{'userToken':localStorage.userToken},	  
		 	data:"{'orderinfo':{'orderAddress':'深圳罗湖','paymentMethod':'AliPay','orderAdcode':'440301'},"
		 		+"'commodity':[{'commodityNumber':'123','buyAmount':'1'},{'commodityNumber':'456','buyAmount':'1'}],"
		 		+"'incidental':[{'commodityNumber':'123','buyAmount':'1'},{'commodityNumber':'456','buyAmount':'1'}]}",
		 	success:function(data){	
		   //data = eval('('+data+')');
		     	var result = data.rows[0];
		      	$('#main-tab').tabs('close',title);
		      	openPage(title,'',baseUrl+result.moUrl,null);
			},
			error:function(xhr,type,errorThrown){
				//异常处理；
				console.log(type);
			}
		});
	}
}

function RSubmitOrder () {

	if(localStorage.userToken==undefined){
		console.log("请登陆") 
	}else{
		var orderNumberdsds = $("#restnumber").val();
		$.ajax({
		  	type:"post",
		  	contentType: "application/json", //必须有
		  	url: URL_all+'client/orderinfo/againneworder',
		  /*	ders:{'userToken':localStorage.userToken},*/
			headers:{'userToken':localStorage.userToken},
			
			data:"{'orderNumber':'"+orderNumberdsds+"'}",
		 	success:function(data){
				console.log(data.dataResultObj);
				if (typeof WeixinJSBridge == "undefined"){
				   	if( document.addEventListener ){
				       document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
				   	}else if (document.attachEvent){
				       document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
				       document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
				   	}
				}else{
					
						$("#restnumberst").val("已点")	
					
					
					resetonBridgeReady(data);
				}
			},
			error:function(xhr,type,errorThrown){
				//异常处理；
				console.log(type);
			}
        });
	}
	
	
	
}



function wechatrefund() {
	var a =$("#tko").val();
	var b  =$("#tkj").val();
	var c =$("#tkz").val();
	$.ajax({
  		type:"post",
  		contentType: "application/json", //必须有
  		url: URL_all+'api/wechat/pay/wechatrefund',
		data:"{'orderNumber':'"+a+"','refundFee':'"+b+"','paymentAmount':'"+c+"'}",
  		success: function(data){
            console.log(data)   
			$("#ordersts").val(data.htmlState)				 
        } 
   });
}




function userToken() {
	console.log("localStorage.userToken"+localStorage.userToken);
	$("#userToken").val("t:"+localStorage.userToken)
	
}




function duserToken() {
 localStorage.removeItem('userToken');
	
}




function onBridgeReady(data){
	console.log(data);
   WeixinJSBridge.invoke(
       'getBrandWCPayRequest', {
           "appId":data.dataResultObj.dataResultObj.appId,     //公众号名称，由商户传入     
           "timeStamp":data.dataResultObj.dataResultObj.timeStamp,         //时间戳，自1970年以来的秒数     
           "nonceStr":data.dataResultObj.dataResultObj.nonceStr, //随机串     
           "package":data.dataResultObj.dataResultObj.prepayId,     
           "signType":"MD5",         //微信签名方式：     
           "paySign":data.dataResultObj.dataResultObj.paySign //微信签名 
       },
       function(res){     
           if(res.err_msg == "get_brand_wcpay_request:ok" ) {
			  	$("#ordersts").val("支付成功")
		
			  
			   
			   }  
           else if (res.err_msg == "get_brand_wcpay_request:cancel") {
  	$("#ordersts").val("已取消支付")		
		
		}    // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。 
       }




   ); 
}




function resetonBridgeReady(data){
	console.log(data);
   WeixinJSBridge.invoke(
       'getBrandWCPayRequest', {
           "appId":data.dataResultObj.appId,     //公众号名称，由商户传入     
           "timeStamp":data.dataResultObj.timeStamp,         //时间戳，自1970年以来的秒数     
           "nonceStr":data.dataResultObj.nonceStr, //随机串     
           "package":data.dataResultObj.prepayId,     
           "signType":"MD5",         //微信签名方式：     
           "paySign":data.dataResultObj.paySign //微信签名 
       },
       function(res){     
           if(res.err_msg == "get_brand_wcpay_request:ok" ) {
			  	$("#ordersts").val("支付成功")
		
			  
			   
			   }  
           else if (res.err_msg == "get_brand_wcpay_request:cancel") {
  	$("#ordersts").val("已取消支付")		
		
		}    // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。 
       }




   ); 
}